﻿using LoveEat.Infrastructure.Interfaces;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoveEat.Infrastructure.Implements
{
    public class BaseRespository<T> : IBaseRespository<T> where T : class
    {
        private readonly MyDbcontext context;

        public BaseRespository(MyDbcontext context)
        {
            this.context = context;
        }
        public MyDbcontext DB { get{ return context; } }

        public async Task<int> AddAsync(T t)
        {
            await context.Set<T>().AddAsync(t);
            return await context.SaveChangesAsync();
        }

        public async Task<int> AddRange(IEnumerable<T> t)
        {
            await context.Set<T>().AddRangeAsync(t);
            return await context.SaveChangesAsync();
        }

        public IQueryable<T> GetAll()
        {
            return context.Set<T>().AsQueryable();
        }

        public async Task<int> UpdateAsync(T t)
        {
            context.Set<T>().Update(t);
            return await context.SaveChangesAsync();
        }

        public async Task<int> UpdateRange(IEnumerable<T> t)
        {
            context.Set<T>().UpdateRange(t);
            return await context.SaveChangesAsync();
        }
    }
}
